package com.cloudkinto.controller.workOrder;


import com.cloudkinto.base.BaseController;
import com.cloudkinto.bo.workOrder.WorkOrderAddBo;
import com.cloudkinto.bo.workOrder.WorkOrderQueryBo;
import com.cloudkinto.bo.workOrder.WorkOrderUpdateBo;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.service.workOrder.WorkOrderService;
import com.cloudkinto.vo.workOrder.WorkOrderDetail;
import com.cloudkinto.vo.workOrder.WorkOrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 工单 前端控制器
 * </p>
 *
 * @author long
 * @since 2025-01-07
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "oms工单模块")
@RequestMapping("/api/admin/oms/workOrder")
public class WorkOrderOmsController extends BaseController {
    private final WorkOrderService workOrderService;

    @UserSessionRequired
    @PostMapping
    @ApiOperation(value = "工单新增", notes = "工单新增")
    public SingleResult add(@RequestBody @Validated WorkOrderAddBo req) {
        return SingleResult.success("新增成功", workOrderService.add(req, getCurrentUserId(), getUserName()));
    }

    @UserSessionRequired
    @PutMapping
    @ApiOperation(value = "工单修改", notes = "工单修改")
    public SingleResult update(@RequestBody @Validated WorkOrderUpdateBo req) {
        return SingleResult.success("修改成功", workOrderService.update(req, getCurrentUserId(), getUserName()));
    }

    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "工单根据id查看详情", notes = "工单根据id查看详情")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = WorkOrderDetail.class))
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", workOrderService.detail(id));
    }

    @UserSessionRequired
    @PutMapping("/cancel/{id}")
    @ApiOperation(value = "取消", notes = "取消")
    public SingleResult cancel(@PathVariable("id") Long id) {
        return SingleResult.success("取消成功", workOrderService.cancel(id, getCurrentUserId(), getUserName()));
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "工单查询初始化", notes = "工单查询初始化")
    public SingleResult pageInit() {
        return workOrderService.pageInit(getCurrentUserId(), getTenantId(), getCompanyId(), "oms");
    }

    @UserSessionRequired
    @PostMapping("/listPage")
    @ApiOperation(value = "工单查询（分页）", notes = "工单查询（分页）")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = WorkOrderVo.class))
    public PageResult listPage(@RequestBody @Validated WorkOrderQueryBo bo) {
        bo.setCompanyId(getCompanyId());
        return workOrderService.getListPage(bo);
    }

    @UserSessionRequired
    @PostMapping("/listPageStatusSta")
    @ApiOperation(value = "工单查询分页状态统计", notes = "工单查询分页状态统计")
    public SingleResult listPageStatusSta(@RequestBody @Validated WorkOrderQueryBo bo) {
        bo.setCompanyId(getCompanyId());
        bo.setStatus(null);
        return SingleResult.success(workOrderService.listPageStatusSta(bo));
    }
}

